package com.kf.lib_network.retrofit

import com.kf.lib_network.BuildConfig
import com.kf.lib_network.http.HttpFactory
import com.zyj.retrofit.adapter.FlowCallAdapterFactory
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory


class RetrofitFactory {
    companion object {
        private const val BASE_URL = BuildConfig.BASE_URL
        val tokenRetrofit: Retrofit by lazy {
            builder.client(
                HttpFactory().createOkHttp(HttpType.TOKEN)
            ).build()
        }
        val signRetrofit: Retrofit by lazy {
            builder.client(
                HttpFactory().createOkHttp(HttpType.SIGN)
            ).build()
        }
        val tokenSignRetrofit: Retrofit by lazy {
            builder.client(
                HttpFactory().createOkHttp(HttpType.TOKEN_SIGN)
            ).build()
        }
        val noneRetrofit: Retrofit by lazy {
            builder.client(
                HttpFactory().createOkHttp(HttpType.NONE)
            ).build()
        }

        private val builder: Retrofit.Builder = Retrofit.Builder()
            .addCallAdapterFactory(FlowCallAdapterFactory.create())
            .addConverterFactory(GsonConverterFactory.create())
            .baseUrl(BASE_URL)
    }


    fun createRetrofit(type: HttpType): Retrofit {
        lateinit var retrofit: Retrofit
        when (type) {
            HttpType.TOKEN -> {
                retrofit = tokenRetrofit
            }
            HttpType.SIGN -> {
                retrofit = signRetrofit
            }

            HttpType.TOKEN_SIGN -> {
                retrofit = tokenSignRetrofit
            }
            HttpType.NONE -> {
                retrofit = noneRetrofit
            }
        }
        return retrofit
    }

}